define(["jquery", "easy-admin"], function ($, ea) {
    table = layui.table;
    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'order/analytics',
        export_url: 'device/export',
    };

    var Controller = {
        index: function () {
            ea.listen();
        },personindex: function () {
            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
        sale: function () {
            ea.listen();
        },
        myedit:function (){
            ea.listen();
        },
        analytics:function (){
            var myDate = new Date();
            var year=myDate.getFullYear(); //获取完整的年份(4位,1970-????)
            var month=myDate.getMonth()+1; //获取当前月份(0-11,0代表1月)
            var cur_date=year+'年'+month+'日'
            ea.table.render({
                toolbar: ['refresh'],
                init: init, limit: Number.MAX_VALUE,page: false,
            defaultToolbar:['filter',{
                    title: '自定义打印',
                    layEvent: 'DIY_PRINT',
                    icon: 'layui-icon-print',
                }],
                cols: [[
                    {search:'<div class="layui-form-item layui-inline"><label class="layui-form-label">月份</label><div class="layui-input-inline"><input id="c-month" name="month" data-date data-date-type="month" class="layui-input" data-search-op="=" value="'+year+'-'+month+'" /></div>',hide:true},
                    {field: 'username', title: '业务员',search:true},

                    {field: 'mubiao', title: '月目标(元)'},
                    {field: 'money_month', title: '已成交(元)',templet: function (res,option){
                            if(res.money_month){
                                return res.money_month;
                            }
                            return 0;
                        }},
                    {field: 'return_money_month', title: '回款（元）',templet: function (res,option){
                            if(res.freight_month){
                                return res.freight_month;
                            }
                            return 0;
                        }},
                    {field: 'wanchenglv', title: '完成率（%）',templet: function (res,option){
                            if(res.money_month){
                                wclv=res.money_month/res.mubiao*100;
                                return wclv.toFixed(2);
                            }
                            return 0;
                        }},
                    {field: 'number_month', title: '已成交（单）',templet: function (res,option){
                            if(res.number_month){
                                return res.number_month;
                            }
                            return 0;
                        }},
                    {field: 'ticheng', title: '提成点（%）',templet: function (res,option){
                            if(res.ticheng){
                                return res.ticheng+'%';
                            }
                            return '';
                        }}
                ]],

                done: function(res, curr, count){
                    if(res.cur_date){
                        cur_date=res.cur_date;
                    }
                    if(count===undefined && res.msg && res.url){
                        ea.msg.tips(res.msg,1,function (){
                            window.top.location.href=res.url;
                        })
                    }
                }
            });

            ea.listen();
            table.on('toolbar(currentTableRenderId_LayFilter)', function (obj) {
                console.log(obj.config.id);
                // 搜索表单的显示
                switch (obj.event) {
                    case 'DIY_PRINT':
                        //自定义打印处理
                        var f = ["<style>", "body{font-size: 12px; color: #666;}", "table{width: 100%; border-collapse: collapse; border-spacing: 0;}", "th,td{line-height: 20px; padding: 9px 15px; border: 1px solid #ccc; text-align: left; font-size: 12px; color: #666;text-align: center;}", "a{color: #666; text-decoration:none;}", "*.layui-hide{display: none}", "</style>"].join("");
                        v=$($(".layui-table-header").html());
                        v.append($(".layui-table-main table").html());
                        v.find("th.layui-table-patch").remove(), v.find(".layui-table-col-special").remove();
                        var h = window.open("Print_window", "_blank");
                        h.document.write(f +'<h1 style="text-align: center;">'+cur_date+'员工业绩表<h1>'+ $(v).prop("outerHTML"));
                        h.document.close();
                        h.print();
                        h.close();
                }
            });
            $('.table-search-fieldset').removeClass('layui-hide');
        }
    };
    return Controller;
});